package customfields;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import util.LookAndFeel;

public class ButtonUADE extends Field{

	private int backgroundColour = LookAndFeel.button_background_color;  
	private int highlightColour = LookAndFeel.button_highlight_color;  
	private int textColour = LookAndFeel.button_text_color;
	private int fieldWidth;  
	private int fieldHeight;  
	private String text;  
	private int padding = 8;  

	public ButtonUADE(String text, long style) {  
		super(style | Field.FOCUSABLE);  
		this.text = text;  
		Font defaultFont = Font.getDefault();  
		fieldHeight = defaultFont.getHeight() + padding;  
		fieldWidth = defaultFont.getAdvance(text) + (padding * 2);  
		this.setPadding(2, 2, 2, 2);  
	}  
	public ButtonUADE(String text) {  
		super(Field.FOCUSABLE);  
		this.text = text;  
		Font defaultFont = Font.getDefault();  
		fieldHeight = defaultFont.getHeight() + padding;  
		fieldWidth = defaultFont.getAdvance(text) + (padding * 2);  
		this.setPadding(2, 2, 2, 2);  
	}  
	protected boolean navigationClick(int status, int time) {  
		fieldChangeNotify(1);  
		return true;  
	}  

	protected void onFocus(int direction) {
		backgroundColour = highlightColour; 
		invalidate();  
	}  

	protected void onUnfocus() {  
		backgroundColour = LookAndFeel.button_background_color;  
		invalidate();  
	}  

	public int getPreferredWidth() {  
		return fieldWidth;  
	}  

	public int getPreferredHeight() {  
		return fieldHeight;  
	}  

	protected void layout(int arg0, int arg1) {  
		setExtent(getPreferredWidth(), getPreferredHeight());  
	}  

	protected void drawFocus(Graphics graphics, boolean on) {  
	}  

	protected void fieldChangeNotify(int context) {  
		try  {  
			this.getChangeListener().fieldChanged(this, context);  
		}  
		catch (Exception e)  
		{}  
	}  

	protected void paint(Graphics graphics) {  
		//background
		graphics.setColor(backgroundColour);  
		graphics.fillRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);
		//border
		graphics.setColor(textColour);
		graphics.drawText(text, padding - 1, padding / 2 + 1);  
		// text
		graphics.setColor(textColour);  
		graphics.drawRoundRect(0, 0, fieldWidth, fieldHeight, 8, 8);  
	}  

}
